package com.gtw.modules.sys.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbCategoryService;
import com.gtw.modules.entity.TbCategory;
import com.gtw.modules.sys.service.SysMemberCommodityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gtw.modules.entity.MemberCommodityEntity;


/**
 * 会员专区商品表
 * 
 * @author wuye
 * @email www.baidu.com
 * @date 2018-06-11 12:06:09
 */
@RestController
@RequestMapping("/sys/membercommodity")
public class SysMemberCommodityController extends AbstractController{
	@Autowired
	private SysMemberCommodityService memberCommodityService;

	@Autowired
	private TbCategoryService categoryService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:membercommodity:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<MemberCommodityEntity> memberCommodityList = memberCommodityService.queryList(query);
		int total = memberCommodityService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(memberCommodityList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:membercommodity:info")
	public R info(@PathVariable("id") Integer id){
		MemberCommodityEntity memberCommodity = memberCommodityService.queryObject(id);
		
		return R.ok().put("memberCommodity", memberCommodity);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:membercommodity:save")
	public R save(@RequestBody MemberCommodityEntity memberCommodity){
		TbCategory tbCategory = categoryService.selectByPrimaryKey(memberCommodity.getCategoryid());
		memberCommodity.setCategoryname(tbCategory.getCategoryyname());
		memberCommodity.setCreatedate(new Date());
		memberCommodity.setUpauthor(getUser().getUsername());
		memberCommodity.setIsdelete(false);
		memberCommodity.setPrice(memberCommodity.getPrice()*100);
		memberCommodity.setPricediscounts(memberCommodity.getPricediscounts()*100);
		memberCommodity.setFreight(memberCommodity.getFreight()*100);
		memberCommodityService.save(memberCommodity);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:membercommodity:update")
	public R update(@RequestBody MemberCommodityEntity memberCommodity){
		memberCommodityService.update(memberCommodity);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:membercommodity:delete")
	public R delete(@RequestBody Integer[] ids){
		memberCommodityService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
